// 数据库迁移脚本：添加description字段到articles表
const { pool } = require('../src/config/database');

async function addDescriptionField() {
  try {
    console.log('开始执行数据库迁移：添加description字段...');
    
    // 检查字段是否已存在
    const checkQuery = `
      SELECT column_name 
      FROM information_schema.columns 
      WHERE table_name = 'articles' AND column_name = 'description'
    `;
    
    const checkResult = await pool.query(checkQuery);
    
    if (checkResult.rows.length > 0) {
      console.log('✅ description字段已存在，跳过迁移');
      return;
    }
    
    // 添加description字段
    const alterQuery = `
      ALTER TABLE articles ADD COLUMN description TEXT;
    `;
    
    await pool.query(alterQuery);
    
    // 添加注释
    const commentQuery = `
      COMMENT ON COLUMN articles.description IS '作品描述，用于简要介绍作品内容';
    `;
    
    await pool.query(commentQuery);
    
    console.log('✅ 成功添加description字段到articles表');
    
  } catch (error) {
    console.error('❌ 数据库迁移失败:', error);
    throw error;
  } finally {
    await pool.end();
  }
}

// 执行迁移
addDescriptionField()
  .then(() => {
    console.log('🎉 数据库迁移完成');
    process.exit(0);
  })
  .catch((error) => {
    console.error('💥 数据库迁移失败:', error);
    process.exit(1);
  });